package com.xckj.cycling.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.xckj.cycling.R;
import com.xckj.cycling.bean.UserinfoBean;
import com.xckj.cycling.databinding.ActivityLoginBinding;
import com.xckj.cycling.framework.BaseActivity;
import com.xckj.cycling.global.Constant;
import com.xckj.cycling.presenter.LoginPresenter;
import com.xckj.cycling.utils.AndroidUtil;
import com.xckj.cycling.utils.BaseLoginUtil;
import com.xckj.cycling.utils.CheckFormatUtil;
import com.xckj.cycling.utils.FacebookLoginUtils;
import com.xckj.cycling.utils.GoogleLoginUtils;
import com.xckj.cycling.utils.SpUtil;
import com.xckj.cycling.iview.ILoginView;

import java.util.Objects;

/**
 * @Author: ChenYao
 * @Describe: 登录
 * @Date: 2023/2/25 23:40
 */
public class LoginActivity extends BaseActivity<ActivityLoginBinding> implements ILoginView {
    private LoginPresenter loginPresenter;
    BaseLoginUtil utils;

    @Override
    protected boolean isStatusBarDarkFont() {
        return false;
    }

    @Override
    protected int loadLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initData() {
        loginPresenter = new LoginPresenter(this);
//        if (BuildConfig.DEBUG) {
//            dataBinding.etMailbox.setText("1341417477@qq.com");
//            dataBinding.etPassword.setText("li123456");
//        }
        String last = SpUtil.getString(this, Constant.LAST_EMAIL);
        if (!TextUtils.isEmpty(last)) {
            dataBinding.etMailbox.setText(last);
        }
        String tips1 = getString(R.string.privacy_1);
        String tips2 = getString(R.string.privacy_2);
        String tips = getString(R.string.privacy_tips, tips1, tips2);
        SpannableStringBuilder stringBuilder = new SpannableStringBuilder(tips);
        //        stringBuilder.setSpan(new ForegroundColorSpan(color),index ,index+tips1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        int color = Color.parseColor("#0AD39F");
        int index = tips.indexOf(tips1);
        stringBuilder.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                BrowserActivity.start(LoginActivity.this, "yinsixieyi");
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(color);
            }
        }, index, index + tips1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        index = tips.indexOf(tips2);
        stringBuilder.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                BrowserActivity.start(LoginActivity.this, "userxieyi");
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(color);
            }
        }, index, index + tips2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        dataBinding.privacy.setText(stringBuilder);
        dataBinding.privacy.setMovementMethod(LinkMovementMethod.getInstance());
        getLoginViewModel().getUserLiveData().observe(this, new Observer<UserinfoBean>() {
            @Override
            public void onChanged(UserinfoBean userinfoBean) {
                internalSuccess(userinfoBean);
            }
        });
        dataBinding.privacy.setOnClickListener(onClickListener);
        dataBinding.ivPrivacy.setOnClickListener(onClickListener);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean select = dataBinding.ivPrivacy.isSelected();
            dataBinding.ivPrivacy.setSelected(!select);
        }
    };

    private void internalSuccess(UserinfoBean userinfo) {
        if (userinfo == null) {
            return;
        }
        if (userinfo.isNew() == 1) {
            SpUtil.put(this, Constant.TOKEN, userinfo.getToken());
            startActivity(new Intent(this, PerfectRegisterActivity.class));
        } else {
            SpUtil.put(this, Constant.TOKEN, userinfo.getToken());
            SpUtil.put(this, Constant.USER_NICK_NAME, userinfo.getName());
            SpUtil.put(this, Constant.USER_AVATAR, userinfo.getAvatar());
            SpUtil.put(this, Constant.USER_ID, String.valueOf(userinfo.getUser_id()));
            startActivity(new Intent(this, MainActivity.class));
        }
        finish();
    }

    @Override
    protected void setListener() {
        dataBinding.btnLogin.setOnClickListener(this);
        dataBinding.btnRegister.setOnClickListener(this);
        dataBinding.btnForgetPassword.setOnClickListener(this);
        dataBinding.btnFacebookLogin.setOnClickListener(this);
        dataBinding.btnGoogleLogin.setOnClickListener(this);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        if (AndroidUtil.isFastDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.btn_login:
                login();
                break;
            case R.id.btn_register:
                register();
                break;
            case R.id.btn_forget_password:
                forgetPassword();
                break;
            case R.id.btn_facebook_login:
                facebookLogin();
                break;
            case R.id.btn_google_login:
                googleLogin();
                break;
            default:
                break;
        }

    }

    private void login() {
        String mailbox = dataBinding.etMailbox.getText().toString();
        String password = Objects.requireNonNull(dataBinding.etPassword.getText()).toString();
        boolean select = dataBinding.ivPrivacy.isSelected();
        if (!select) {
            showMessage(getString(R.string.privacy_agree, getString(R.string.privacy_1), getString(R.string.privacy_2)));
            return;
        }
        if (TextUtils.isEmpty(mailbox)) {
            showMessage(getString(R.string.mailbox_hint));
            return;
        }
        if (!CheckFormatUtil.isMailbox(mailbox)) {
            showMessage(getString(R.string.mailbox_error));
            return;
        }
        if (TextUtils.isEmpty(password)) {
            showMessage(getString(R.string.password_hint));
            return;
        }
        if (password.length() <= Constant.PASSWORD_MIN_LENGTH) {
            showMessage(getString(R.string.password_length_error));
            return;
        }
        login(mailbox, password);

    }

    private void login(String mailbox, String password) {
        loginPresenter.login(mailbox, password);
    }

    private void register() {
        startActivity(new Intent(this, RegisterActivity.class));
    }

    private void forgetPassword() {
        startActivity(new Intent(this, ForgetPasswordActivity.class));
    }

    private void facebookLogin() {
        if (utils == null) {
            utils = new FacebookLoginUtils(this);
        } else {
            utils.onDestroy();
        }
        utils.startLogin();
    }

    private void googleLogin() {
        if (utils == null) {
            utils = new GoogleLoginUtils(this);
        } else {
            utils.onDestroy();
        }
        utils.startLogin();
        showLoading();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (utils != null) {
            utils.onDestroy();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (utils != null) {
            utils.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void loginSuccess(UserinfoBean userinfo) {
        hideDialog();
        getLoginViewModel().getUserLiveData().setValue(userinfo);
    }

    @Override
    public void loginFailure(String message) {
        showMessage(message);
    }
}
